<?php

/**
 * This is the model class for table "iteam_user".
 *
 * The followings are the available columns in table 'iteam_user':
 * @property string $uid
 * @property string $avatar
 * @property string $username
 * @property string $realname
 * @property string $nickname
 * @property integer $sex
 * @property integer $blood
 * @property string $idcard
 * @property string $birthday
 * @property string $email
 * @property integer $email_status
 * @property string $mobile
 * @property integer $mobile_status
 * @property string $postcode
 * @property string $address
 * @property integer $occupation
 * @property integer $salary
 * @property integer $education
 * @property integer $marital
 * @property string $password
 * @property integer $hongyun
 * @property integer $coin
 * @property string $hy_expiry
 * @property string $coin_expiry
 * @property integer $status
 * @property string $dateline
 * @property string $updated
 */
class User extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return User the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table nameunique
     */
    public function tableName() {
        return 'iteam_user';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('username, idcard, birthday, email, mobile', 'required'),
            array('sex, blood, email_status, mobile_status, occupation, salary, education, marital, hongyun, coin, status', 'numerical', 'integerOnly' => true),
            array('avatar, username, realname, nickname, idcard, address, password', 'length', 'max' => 100),
            array('email', 'length', 'max' => 200),
            array('mobile, postcode', 'length', 'max' => 50),
            array('hy_expiry, coin_expiry, dateline, updated', 'safe'),
            //各種正則驗證
            array('username', 'match', 'pattern' => '/^[a-zA-Z0-9]{4,16}$/','message'=>'帳號請填入4至16個字元，組成限英文、數字。'),
            array('realname', 'match', 'pattern'=>'/^[\x{4e00}-\x{9fa5}]{2,6}$/u', 'message'=>'請填真實姓名。'),
            array('email', 'email', 'message'=>'請輸入正確的郵箱地址。'),
            array('mobile', 'match', 'pattern' => '/^(09[0-9]{8})$/' , 'message'=>'請輸入正確的手機號碼。'),
            array('birthday', 'match', 'pattern' => '/^([1-2][0-9]{3}-[0-1][0-9]-[0-3][0-9])$/' , 'message'=>'出生年月日 請輸入正確的日期。'),  
            array('username, email, mobile, idcard', 'unique'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('uid, avatar, username, realname, nickname, sex, blood, idcard, birthday, email, email_status, mobile, mobile_status, postcode, address, occupation, salary, education, marital, password, hongyun, coin, hy_expiry, coin_expiry, status, dateline, updated', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'uid' => 'UID',
            'avatar' => 'Avatar',
            'username' => '帳號',
            'realname' => '姓名',
            'nickname' => '昵稱',
            'sex' => '性別',
            'blood' => '血型',
            'idcard' => '身份證號碼',
            'birthday' => '生日',
            'email' => 'E-mail',            
            'email_status' => '郵件狀態',
            'mobile' => '手機',
            'mobile_status' => '手機狀態',
            'postcode' => '郵政編碼',
            'address' => '通訊地址',
            'occupation' => '職業',
            'salary' => '薪資',
            'education' => '學歷',
            'marital' => '婚姻',
            'password' => '密碼',
            'hongyun' => '鴻運點數',
            'coin' => '購物金',
            'hy_expiry' => '鴻運點數過期時間',
            'coin_expiry' => '購物金過期時間',
            'status' => '用戶狀態',
            'dateline' => '創建時間',
            'updated' => '更新時間',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('uid', $this->uid, true);
        $criteria->compare('avatar', $this->avatar, true);
        $criteria->compare('username', $this->username, true);
        $criteria->compare('realname', $this->realname, true);
        $criteria->compare('nickname', $this->nickname, true);
        $criteria->compare('sex', $this->sex);
        $criteria->compare('blood', $this->blood);
        $criteria->compare('idcard', $this->idcard, true);
        $criteria->compare('birthday', $this->birthday, true);
        $criteria->compare('email', $this->email, true);
        $criteria->compare('email_status', $this->email_status);
        $criteria->compare('mobile', $this->mobile, true);
        $criteria->compare('mobile_status', $this->mobile_status);
        $criteria->compare('postcode', $this->postcode, true);
        $criteria->compare('address', $this->address, true);
        $criteria->compare('occupation', $this->occupation);
        $criteria->compare('salary', $this->salary);
        $criteria->compare('education', $this->education);
        $criteria->compare('marital', $this->marital);
        $criteria->compare('password', $this->password, true);
        $criteria->compare('hongyun', $this->hongyun);
        $criteria->compare('coin', $this->coin);
        $criteria->compare('hy_expiry', $this->hy_expiry, true);
        $criteria->compare('coin_expiry', $this->coin_expiry, true);
        $criteria->compare('status', $this->status);
        $criteria->compare('dateline', $this->dateline, true);
        $criteria->compare('updated', $this->updated, true);

        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                ));
    }
    
    protected function beforeSave() {
        if (parent::beforeSave()) {
            if ($this->isNewRecord) {
                $this->dateline = $this->updated = date('Y-m-d H:i:s');
                if(!$this->avatar){
                    $this->avatar = 'noavatar.jpg';
                }
            }
            $this->updated = date('Y-m-d H:i:s');
        }
        return true;
    }

    public function validatePassword($password) {
        return md5($password) === $this->password;
    }

}